package io.geekidea.springbootplus.system.controller;

import cn.hutool.crypto.Padding;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.annotations.CurrentAdmin;
import io.geekidea.springbootplus.system.annotations.CurrentAdminInfo;
import io.geekidea.springbootplus.system.entity.ParcelCenter;
import io.geekidea.springbootplus.system.entity.ParcelCenterCo;
import io.geekidea.springbootplus.system.param.ParcelCenterCoPageParam;
import io.geekidea.springbootplus.system.service.ParcelCenterCoService;
import io.geekidea.springbootplus.system.service.ParcelCenterService;
import io.geekidea.springbootplus.system.vo.ParcelCenterCoQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  控制器
 *
 * @author miko
 * @since 2020-10-24
 */
@Slf4j
@RestController
@RequestMapping("/parcelCenterCo")
@Module("system")
@Api(value = "API", tags = {""})
public class ParcelCenterCoControllerExtra extends BaseController {

    @Autowired
    private ParcelCenterCoService parcelCenterCoService;

    @Autowired
    private ParcelCenterService parcelCenterService;


    /**
     * 添加
     */
    @PostMapping("/change")
    @OperationLog(name = "添加", type = OperationLogType.ADD)
    @ApiOperation(value = "添加", response = ApiResult.class)
    public ApiResult<Boolean> addParcelCenter(@RequestBody JSONObject jsonObject,@CurrentAdmin CurrentAdminInfo currentAdminInfo) throws Exception {
        int CenterID = jsonObject.getInteger("CenterID");
        String CoName = jsonObject.getString("CoName");

        LambdaQueryWrapper<ParcelCenterCo> wrapper = new LambdaQueryWrapper<ParcelCenterCo>();
        wrapper.eq(ParcelCenterCo::getCoName,CoName)
                .eq(ParcelCenterCo::getSchoolID,currentAdminInfo.getSchool().getSchoolID());

        parcelCenterCoService.remove(wrapper);

        ParcelCenterCo parcelCenterCo = new ParcelCenterCo();
        parcelCenterCo.setCoName(CoName);
        parcelCenterCo.setCreateTime(new Date());
        parcelCenterCo.setDeleted(0);
        parcelCenterCo.setSchoolID(currentAdminInfo.getSchool().getSchoolID());
        parcelCenterCo.setCenterID(CenterID);
        ParcelCenter parcelCenter = parcelCenterService.getById(CenterID);
        parcelCenterCo.setName(parcelCenter.getName());

        parcelCenterCoService.saveParcelCenterCo(parcelCenterCo);

        return ApiResult.result(true);
    }

    /**
     * 分页列表
     * @return
     */
    @PostMapping("/getBySchool")
    @OperationLog(name = "分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "分页列表", response = ParcelCenterCoQueryVo.class)
    public ApiResult<Paging<ParcelCenterCo>> getParcelCenterCoPageList(@Validated @RequestBody ParcelCenterCoPageParam parcelCenterCoPageParam) throws Exception {
        List<ParcelCenter> list = parcelCenterService.list(new LambdaQueryWrapper<ParcelCenter>().eq(ParcelCenter::getSchoolID,parcelCenterCoPageParam.getKeyword()));

        List<ParcelCenterCo> list1 = new ArrayList<>();

        list.forEach(item->{
            ParcelCenterCo parcelCenterCo = new ParcelCenterCo();
            parcelCenterCo.setCoName(item.getName());

            ParcelCenterCo a = parcelCenterCoService
                    .getOne(new LambdaQueryWrapper<ParcelCenterCo>()
                            .eq(ParcelCenterCo::getSchoolID,parcelCenterCoPageParam.getKeyword())
                            .eq(ParcelCenterCo::getCoName,item.getName()).eq(ParcelCenterCo::getDeleted,0));

            if(a != null){
                parcelCenterCo.setName(a.getName());
            }

            list1.add(a);
        });
        Paging<ParcelCenterCo> paging = new Paging<ParcelCenterCo>();
        paging.setRecords(list1);
        return ApiResult.ok(paging);
    }


}
